Java Programming Language
Java Logic Statement Syntax Exercises #1

These exercises are designed to evaluate your knowledge, comprehension, application and analysis of the Java logic statement syntax. It is expected that you should be able to correctly answer all or nearly all of the questions and problems presented. You should do these exercises by yourself.

1. Lookup Unicode values at the http://charts.unicode.org/Web.html website.

Character

Unicode Value

p

 
 

$03C0

 

$056D

?

 

 

2. Identify valid and invalid comments and why not valid.

 

Valid or Invalid

Why not valid

/* is this a valid comment? */

   

// is this a valid comment?

   

//* is this a valid comment?

   

*/ is this a valid comment? /*

   

// is this a

valid comment?

   

/** is this a valid comment? **/

   

/* is this

a valid

comment

*/

   

// is this a valid comment? //

   

// is this a valid

comment? */

   

/** is this a valid comment? */

   

/* is this a valid comment? //

   

 

3. Identify valid and invalid identifiers and why not valid.

 

Valid or Invalid

Why not valid

int

   

myInt

   

3cpo

   

$hex

   

period.ending

   

r2d2

   

uncle$scrooge

   

DumbVarIABle

   

dollars$

   

percent%sign

   

 

4. Identify the values into the Java type that it most accurately represents. Take note of the format of the variable values and their range is relation to the Java definitions of types given in the lecture material to make your decision.

Value

Type

123086

 

True

 

0

 

0xF0001320DC657AA9

 

"Hello World"

 

10000000

 

-543287.12032222

 

-436

 

‘Z’

 

255

 

5,000,000,000

 

0377

 

‘?’

 

0x0FED1200

 

False

 

-3,000,000,000

 

"Java"

 

255L

 

.0001D

 

0xffL

 

4523.346F

 

 

5. Identify valid and invalid literals and their type and why not valid.

 

Type

Valid or Invalid

Why not valid

+123

     

"Hello there"

     

238

     

-4356

     

65535

     

-32768

     

3.14F

     

'c'

     

-0.000023

     

+6.0

     

'why me?'

     

"z"

     

true

     

off

     

FALSE

     

null

     

453,645,786,864

     

\0000

     

017

     

0xffL

     

3.14159D

     

 

6. Given the following variable declarations, convert or cast the variable into the specified type and fill in the resulting type and value column. Incorporate any automatic promotion to int that is done. If the conversion or casting is invalid then indicate that is so by stating why.

Given

Convert or Cast to

Result Type & Value

Why Invalid

int a = 128;

byte

   

short s = 4096;

int

   

int a = 112;

int

   

char c = 'A';

byte

   

boolean b = true;

int

   

short s = -23456;

double

   

int a = 127;

byte

   

byte b = -128;

long

   

int a = 1,232,598

float

   

float f = -3.14

boolean

   

char c '/u03c0'

short

   

double d = 0.0001

long

   

char c = '2'

int

   

float f = -542.232

double

   

long a = -34

double

   

short s = 0xFF00

char

   

long a = -1024

short

   

float f = 1.1

byte

   

byte b = 13;

Short